package php

import (
    "wxsdk/miniapp/core"
	"dhfshop/models/isv"
	"dhfshop/controllers/base"
	"encoding/json"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/validation"
)

type PhpBaseController struct {
	base.BaseController
}

func (this *PhpBaseController) GetRequsts(phpMethod string) {
	v := struct {
	    Token      string  `form:"token" valid:"Required"`
		Content    string  `form:"content"`
	}{} 
	this.ParseForm(&v)
	valid := validation.Validation{}
	passed, _ := valid.Valid(&v)
	if !passed {
		this.ServeError(valid.Errors[0].Key+" "+valid.Errors[0].Message)
		return 
	}
	req := struct {
	    Method      string  `json:"method"`
		Token       string  `json:"token"`
		CompanyId   int     `json:"company_id"`
		RoleId      int     `json:"role_id"`
		UserId      int     `json:"user_id"`
		Content     string  `json:"content"`
		DhfxId      string  `json:"dhfx_id"`
		IsDealer    int     `json:"is_dealer"`
		AppId       string  `json:"app_id"`
	}{Token:v.Token,Content:v.Content}
	//beego.Debug("content:", v.Content)
	req.Method = phpMethod
	req.CompanyId = this.Ctx.Input.GetData("curCompanyId").(int)
	req.RoleId = this.Ctx.Input.GetData("curRoleId").(int)
	if req.RoleId == 1 {
	    if this.Ctx.Input.GetData("curAppId") != nil {
	       req.AppId = this.Ctx.Input.GetData("curAppId").(string)
	    }
	}
	if req.RoleId == 2 {
	    if this.Ctx.Input.GetData("curDhfxId") != nil {
	       req.DhfxId = this.Ctx.Input.GetData("curDhfxId").(string)
	    }
		if this.Ctx.Input.GetData("curIsDealer") != nil {
	       req.IsDealer = this.Ctx.Input.GetData("curIsDealer").(int)
	    }
	}
	req.UserId = this.Ctx.Input.GetData("curUserId").(int)
	reqJson, err := json.Marshal(&req)
	if err != nil {
		this.ServeError(err.Error())
		return
	}
	reqUrl := beego.AppConfig.String("PHPApiUrl") + "/api/rest"
	client := core.GetPostProxyClient()
	//beego.Debug("reqUrl:", reqUrl, "reqJson:", reqJson)
	retCode, result, err := client.Excute("POST", reqUrl, reqJson)
	if err != nil {
		this.ServeError(err.Error())
		return
	}
	if retCode != 200 {
	    this.Ctx.Output.SetStatus(retCode)
		this.ServeError(string(result))
		this.StopRun()
	}
	this.Ctx.WriteString(string(result))
}

func (this *PhpBaseController) GetPubRequsts(phpMethod string) {
	v := struct {
	    Token      string  `form:"token"`
		Content    string  `form:"content"`
	}{} 
	this.ParseForm(&v)
	valid := validation.Validation{}
	passed, _ := valid.Valid(&v)
	if !passed {
		this.ServeError(valid.Errors[0].Key+" "+valid.Errors[0].Message)
		return 
	}
	req := struct {
	    Method      string  `json:"method"`
		Token       string  `json:"token"`
		CompanyId   int     `json:"company_id"`
		RoleId      int     `json:"role_id"`
		UserId      int     `json:"user_id"`
		Content     string  `json:"content"`
		DhfxId      string  `json:"dhfx_id"`
	}{Token:v.Token,Content:v.Content}
	req.Method = phpMethod
	reqJson, err := json.Marshal(&req)
	if err != nil {
		this.ServeError(err.Error())
		return
	}
	reqUrl := beego.AppConfig.String("PHPApiUrl") + "/api/rest"
	client := core.GetPostProxyClient()
	//beego.Debug("reqUrl:", reqUrl, "reqJson:", reqJson)
	retCode, result, err := client.Excute("POST", reqUrl, reqJson)
	if err != nil {
		this.ServeError(err.Error())
		return
	}
	if retCode != 200 {
	    this.Ctx.Output.SetStatus(retCode)
		this.ServeError(string(result))
        this.StopRun()		
	}
	this.Ctx.WriteString(string(result))
}

func (this *PhpBaseController) GetAppIdRequsts(phpMethod string) {
	 v := struct {
	    Token      string  `form:"token" valid:"Required"`
		Content    string  `form:"content"`
	}{} 
	this.ParseForm(&v)
	valid := validation.Validation{}
	passed, _ := valid.Valid(&v)
	if !passed {
		this.ServeError(valid.Errors[0].Key+" "+valid.Errors[0].Message)
		return 
	}
	req := struct {
	    Method      string  `json:"method"`
		Token       string  `json:"token"`
		AppId       string  `json:"app_id"`
		CompanyId   int     `json:"company_id"`
		RoleId      int     `json:"role_id"`
		UserId      int     `json:"user_id"`
		AccessToken string  `json:"access_token"`
		Content     string  `json:"content"`
	}{Token:v.Token,Content:v.Content}
	req.Method = phpMethod
	req.CompanyId = this.Ctx.Input.GetData("curCompanyId").(int)
	req.RoleId = this.Ctx.Input.GetData("curRoleId").(int)
	if req.RoleId != 1 {
	    beego.Debug("role is error")
		this.ServeError("role is error")
		return
	}
	req.UserId = this.Ctx.Input.GetData("curUserId").(int)
	req.AppId = this.Ctx.Input.GetData("curAppId").(string)
	custAccount, err := isv.GetCustAccountByAppId(req.AppId)  
	if err != nil {
	    this.ServeError(err.Error())
		return
	}
	req.AccessToken = custAccount.AccessToken
	reqJson, err := json.Marshal(&req)
	if err != nil {
		this.ServeError(err.Error())
		return
	}
	reqUrl := beego.AppConfig.String("PHPApiUrl") + "/api/rest"
	client := core.GetPostProxyClient()
	//beego.Debug("reqUrl:", reqUrl, "reqJson:", reqJson)
	retCode, result, err := client.Excute("POST", reqUrl, reqJson)
	if err != nil {
		this.ServeError(err.Error())
		return
	}
	if retCode != 200 {
	    this.Ctx.Output.SetStatus(retCode)
		this.ServeError(string(result))
		this.StopRun()
	}
	this.Ctx.WriteString(string(result))
}

func (this *PhpBaseController) GetFileDownLoad(phpMethod string) {
	v := struct {
	    Token      string  `form:"token" valid:"Required"`
		Content    string  `form:"content"`
	}{} 
	this.ParseForm(&v)
	valid := validation.Validation{}
	passed, _ := valid.Valid(&v)
	if !passed {
		this.ServeError(valid.Errors[0].Key+" "+valid.Errors[0].Message)
		return 
	}
	req := struct {
	    Method      string  `json:"method"`
		Token       string  `json:"token"`
		CompanyId   int     `json:"company_id"`
		RoleId      int     `json:"role_id"`
		UserId      int     `json:"user_id"`
		Content     string  `json:"content"`
		DhfxId      string  `json:"dhfx_id"`
	}{Token:v.Token,Content:v.Content}
	//beego.Debug("content:", v.Content)
	req.Method = phpMethod
	req.CompanyId = this.Ctx.Input.GetData("curCompanyId").(int)
	req.RoleId = this.Ctx.Input.GetData("curRoleId").(int)
	if req.RoleId == 2 {
	    if this.Ctx.Input.GetData("curDhfxId") != nil {
	       req.DhfxId = this.Ctx.Input.GetData("curDhfxId").(string)
	    }
	}
	req.UserId = this.Ctx.Input.GetData("curUserId").(int)
	reqJson, err := json.Marshal(&req)
	if err != nil {
		this.ServeError(err.Error())
		return
	}
	reqUrl := beego.AppConfig.String("PHPApiUrl") + "/api/rest"
	client := core.GetPostProxyClient()
	//beego.Debug("reqUrl:", reqUrl, "reqJson:", reqJson)
	retCode, result, err := client.Excute("POST", reqUrl, reqJson)
	if err != nil {
		this.ServeError(err.Error())
		return
	}
	if retCode != 200 {
	    this.Ctx.Output.SetStatus(retCode)
		this.ServeError(string(result))
		this.StopRun()
	}
	this.Ctx.Output.Header("Content-Type", "application/force-download; application/vnd.ms-excel")
    this.Ctx.Output.Header("Content-Disposition", "filename=order.csv")
	//this.Ctx.WriteString(string(result))
	this.Ctx.ResponseWriter.Write(result)
}

